package com.deuwise.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.system.entity.SysLayerFieldOptional;
import com.deuwise.system.service.SysLayerFieldOptionalService;
import io.swagger.annotations.*;
import io.swagger.annotations.ApiImplicitParams;
import com.deuwise.common.utils.R;
import com.deuwise.common.utils.Result;
import com.baomidou.mybatisplus.plugins.Page;
import com.deuwise.system.entity.SysLayerField;
import com.deuwise.system.service.SysLayerFieldService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *@author hezhenhao
 *@date 2020-05-27 13:26:17
 */
@Api(tags = "图层字段配置模块", description = "图层字段配置")
@RequestMapping("system/sysLayerField")
@RestController()
public class SysLayerFieldController extends AbstractController{
    @Autowired
    private SysLayerFieldService sysLayerFieldService;
    @Autowired
    private SysLayerFieldOptionalService sysLayerFieldOptionalService;

    @ApiOperation(value = "获取list", notes = "")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "titleId", value = "图层弹窗ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "field", value = "字段", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fieldType", value = "字段类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "开始页数", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", defaultValue = "15", dataType = "int", paramType = "query")})
    public Result<Page<SysLayerField>> list(String titleId, String field, String fieldType){
        Wrapper<SysLayerField> wrapper = new EntityWrapper<SysLayerField>();
        wrapper.orderBy("sort", true);
        if(StringUtils.isNotBlank(titleId)){
          wrapper.eq("title_id", titleId);
        }
        if(StringUtils.isNotBlank(field)){
          wrapper.like("field", field);
        }
        if(StringUtils.isNotBlank(fieldType)){
          wrapper.like("field_type", fieldType);
        }
        Page<SysLayerField> listPage = sysLayerFieldService.selectPage(getPage(SysLayerField.class),wrapper);
        for(SysLayerField sysLayerField : listPage.getRecords()){
            Wrapper<SysLayerFieldOptional> wrapperOptional = new EntityWrapper<SysLayerFieldOptional>();
            wrapperOptional.eq("field_id", sysLayerField.getId());
            List<SysLayerFieldOptional> fieldOptionalList = sysLayerFieldOptionalService.selectList(wrapperOptional);
            sysLayerField.setFieldOptionalList(fieldOptionalList);
        }
        return Result.ok(listPage);
    }

    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/insertSyslayerfield", method = RequestMethod.POST)
    public R insertSysLayerField(@RequestBody SysLayerField sysLayerField){
        boolean isSuccess = false;
        if(null == sysLayerField){
            return R.error(200,"新增数据失败，请稍后再试!");
        }
        isSuccess = sysLayerFieldService.insert(sysLayerField);
        if (isSuccess) {
            Wrapper<SysLayerFieldOptional> wrapperOptional = new EntityWrapper<SysLayerFieldOptional>();
            wrapperOptional.eq("field_id", sysLayerField.getId());
            sysLayerFieldOptionalService.delete(wrapperOptional);
            List<SysLayerFieldOptional> optionals = sysLayerField.getFieldOptionalList();
            if(null != optionals && 0 < optionals.size()){
                for(SysLayerFieldOptional optional : optionals){
                    optional.setFieldId(sysLayerField.getId());
                }
                sysLayerFieldOptionalService.insertBatch(optionals);
            }
            return R.ok();
        } else {
            return R.error(200,"新增数据失败，请稍后再试!");
        }
    }

    @ApiOperation("删除字典")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") String id){

        sysLayerFieldService.deleteById(id);

        return R.ok();
    }
}